package selenium;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Clipboard;
import java.awt.Toolkit;

import org.testng.*;
import org.testng.annotations.*;

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;

import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;

public class AddFilmAllFields extends selenium.pages.TestBase {
  private boolean acceptNextAlert = true;
  private StringBuffer verificationErrors = new StringBuffer();

@Test
  public void testFilmAddAllFields() throws Exception {    
	driver.get(baseUrl + "/php4dvd/");
	
	selenium.LoginTest.testLogin();
	
//  Add new film with all fields	
    
	driver.findElement(By.xpath("//a[@href='./?go=add']")).click();
    
    WebElement Number = 
	driver.findElement(By.name("imdbid"));
    Number.click();
	Number.sendKeys("1000");
    
    WebElement Title = 
	driver.findElement(By.name("name"));
    Title.click();
    Title.sendKeys("Fury");
    
    WebElement AlsoKnownAs = 
    driver.findElement(By.name("aka"));
    AlsoKnownAs.click();
    AlsoKnownAs.sendKeys("War never ends quitely");
    
    WebElement Year = 
    driver.findElement(By.name("year"));
    Year.click();
    Year.sendKeys("2014");
    
    WebElement Duration =
    driver.findElement(By.name("duration"));
    Duration.click();
    Duration.sendKeys("134");
    
    WebElement Rating = 
    driver.findElement(By.name("rating"));
    Rating.click();
    Rating.sendKeys("4");
    
    WebElement Format = 
    driver.findElement(By.id("formats"));
    Format.click();
    Format.clear();
    Format.sendKeys("Blu-ray");    
    
    WebElement Own = 
    driver.findElement(By.id("own_no"));
    Own.click();
    
    WebElement Seen = 
    driver.findElement(By.id("seen_no"));
    Seen.click();
    
    WebElement Loaned = 
    driver.findElement(By.id("loaned_yes"));
    Loaned.click();
    
    WebElement LoanedTo = 
    driver.findElement(By.name("loanname"));
    LoanedTo.sendKeys("Tom Jerry");
    
    WebElement LoanedSince = 
    driver.findElement(By.name("loandate"));
    LoanedSince.clear();
    LoanedSince.sendKeys("2014-01-01");    
    
    WebElement Cover = 
    driver.findElement(By.id("cover"));
    Cover.sendKeys("C:\\Users\\korotkova\\Desktop\\Auto\\fury.jpg");
    
    WebElement Trailer = 
    driver.findElement(By.name("trailer"));
    Trailer.click();
    Trailer.sendKeys("http://www.youtube.com/watch?v=SvptrOhxJgk");
    
    WebElement Notes = 
    driver.findElement(By.name("notes"));
    setClipboardContents("Fury is a 2014 American war film set during World War II written and directed by David Ayer.[4] The film stars Brad Pitt, Shia LaBeouf, Logan Lerman, Jon Bernthal, Michael Peña, Jason Isaacs, and Scott Eastwood.");
    Notes.sendKeys(Keys.CONTROL + "v");
    
    WebElement Taglines = 
    driver.findElement(By.name("taglines"));
    Taglines.click();
    Taglines.sendKeys("action, drama");
   
    WebElement PlotOutline = 
    driver.findElement(By.name("plotoutline"));
    setClipboardContents("The surviving crew, which has been together since the North African Campaign, upon meeting the raw recruit despise him both for his lack of experience and for his reluctance to kill Germans, and especially teenagers of the Hitlerjugend, in cold blood; a decision which results in the destruction of one of the Allied tanks and its crew. Wardaddy is furious and in an effort to brutalise the young man and 'educate' him to the realities of war, he violently attempts to force Norman to take his weapon and shoot dead a captive German artilleryman. When Norman refuses to do so, Wardaddy forces the gun on him and makes him execute the prisoner. It is around this point in the film, that it becomes evident that Wardaddy has begun to feel a responsibility for the young man."
    		           + "This bond between Norman and Wardaddy becomes stronger after capturing a small German town, where Wardaddy and Norman meet a German woman and her cousin, named Emma. Norman (presumably) sleeps with Emma, then joins Wardaddy and Emma's cousin for breakfast. However, the rest of the crew barge in and cause tensions while at the table. Shortly afterwards, a German bombardment hits the town, killing Emma and some of the American forces.");
    PlotOutline.click();
    PlotOutline.sendKeys(Keys.CONTROL + "v");
       
    WebElement Plots = 
    driver.findElement(By.name("plots"));
    Plots.click();
    Plots.sendKeys("Plots");
    
    WebElement Languages = 
    driver.findElement(By.id("text_languages_0"));
    Languages.click();
    Languages.sendKeys("russian");
    
    WebElement Subtitles = 
    driver.findElement(By.name("subtitles"));
    Subtitles.click();
    Subtitles.sendKeys("no");
    
    WebElement Audio = 
    driver.findElement(By.name("audio"));
    Audio.click();
    Audio.sendKeys("audio");
    
    WebElement Video = 
    driver.findElement(By.name("video"));
    Video.click();
    Video.sendKeys("video");
    
    WebElement Country = 
    driver.findElement(By.name("country"));
    Country.click();
    Country.sendKeys("USA");
    
    WebElement Genres = 
    driver.findElement(By.name("genres"));
    Genres.click();
    Genres.sendKeys("action");
    
    WebElement Director =
    driver.findElement(By.name("director"));
    Director.click();
    Director.sendKeys("David Ayer");
    
    WebElement Writer = 
    driver.findElement(By.name("writer"));
    Writer.click();
    Writer.sendKeys("David Ayer");
    
    WebElement Producer = 
    driver.findElement(By.name("producer"));
    Producer.click();
    Producer.sendKeys("Bill Block");

    WebElement Music = 
    driver.findElement(By.name("music"));
    Music.click();
    Music.sendKeys("Alex Ott");
    
    WebElement Cast = 
    driver.findElement(By.name("cast"));
    Cast.click();
    Cast.sendKeys("Brad Pitt");
   
    WebElement Save = 
    driver.findElement(By.id("submit"));
    Save.click();
    
 // Check Film correctly added   
    
    WebElement FilmTitle = 
    driver.findElement(By.xpath("//div[@class='maininfo']/h2"));
    Assert.assertTrue(FilmTitle.getText().equals("Fury (2014)"));
    
    WebElement FilmLoan = 
    driver.findElement(By.xpath("//div[@class='loaned']"));
    Assert.assertTrue(FilmLoan.getText().contains("Loaned out to Tom Jerry on 2014-01-01"));    
    
    WebElement FilmGenre = 
    driver.findElement(By.cssSelector("div.genre"));
    Assert.assertTrue(FilmGenre.getText().equals("action"));
    
    WebElement FilmDuration = 
    driver.findElement(By.cssSelector("div.duration"));
    Assert.assertTrue(FilmDuration.getText().equals("Blu-ray, 134 minutes"));
    
    Assert.assertTrue(isElementPresent(By.xpath("//div[@class='photo']/img")));
    
    WebElement FilmTaglines = 
    driver.findElement(By.xpath("//div[@class='taglines']/div"));
    Assert.assertTrue(FilmTaglines.getText().equals("action, drama"));
    
    WebElement FilmRating = 
    driver.findElement(By.cssSelector("div.rating"));
    Assert.assertTrue(FilmRating.getText().contains("4"));
    
    WebElement FilmSubtitles = 
    driver.findElement(By.cssSelector("div.subtitles"));
    Assert.assertTrue(FilmSubtitles.getText().contains("no"));
    
    WebElement FilmCountry = 
    driver.findElement(By.cssSelector("div.country"));
    Assert.assertTrue(FilmCountry.getText().contains("USA"));
    
    WebElement FilmDirector = 
    driver.findElement(By.cssSelector("div.director"));
    Assert.assertTrue(FilmDirector.getText().contains("David Ayer"));
    
    WebElement FilmWriter = 
    driver.findElement(By.cssSelector("div.writer"));
    Assert.assertTrue(FilmWriter.getText().contains("David Ayer"));
    
    WebElement FilmProducer = 
    driver.findElement(By.cssSelector("div.producer"));
    Assert.assertTrue(FilmProducer.getText().contains("Bill Block"));
    
    WebElement FilmMusic = 
    driver.findElement(By.cssSelector("div.music"));
    Assert.assertTrue(FilmMusic.getText().contains("Alex Ott"));
    
    WebElement FilmVideo = 
    driver.findElement(By.cssSelector("div.video"));
    Assert.assertTrue(FilmVideo.getText().contains("video"));
    
    WebElement FilmAudio = 
    driver.findElement(By.cssSelector("div.audio"));
    Assert.assertTrue(FilmAudio.getText().contains("audio"));
    
    WebElement FilmNotes = 
    driver.findElement(By.cssSelector("div.notes"));
    Assert.assertTrue(FilmNotes.getText().contains("Fury is a 2014 American war film"));
    
    WebElement FilmPlotOutline = 
    driver.findElement(By.cssSelector("div.plotoutline"));
    Assert.assertTrue(FilmPlotOutline.getText().contains("some of the American forces"));
    
    WebElement FilmCast = 
    driver.findElement(By.cssSelector("div.cast"));
    Assert.assertTrue(FilmCast.getText().contains("Brad Pitt"));
    
    WebElement FilmPlots = 
    driver.findElement(By.xpath("//div[@class='plots']//li"));
    Assert.assertTrue(FilmPlots.getText().contains("Plots"));     
    
   }

public static void setClipboardContents(String text) {
	  StringSelection stringSelection = new StringSelection( text );
	  Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
	  clipboard.setContents(stringSelection, null);
  
	
}

private boolean isElementPresent(By by) {
    try {
      driver.findElement(by);
      return true;
    } catch (NoSuchElementException e) {
      return false;
    }
  }

  private String closeAlertAndGetItsText() {
    try {
      Alert alert = driver.switchTo().alert();
      String alertText = alert.getText();
      if (acceptNextAlert) {
        alert.accept();
      } else {
        alert.dismiss();
      }
      return alertText;
    } finally {
      acceptNextAlert = true;
    }
  }
  
@AfterMethod
  public static void SiteLogout() throws Exception {
	
	selenium.LogoutTest.testLogout();
	       
  }  
  
}